package com.xinghen.security.core.validate.code.email;

import com.xinghen.security.core.SecurityConstants;
import com.xinghen.security.core.validate.code.ValidateCode;
import com.xinghen.security.core.validate.code.ValidateCodeGenerator;
import com.xinghen.security.core.validate.code.ValidateCodeRepository;
import com.xinghen.security.core.validate.code.impl.AbstractValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Map;

/**
 * @author xinghen
 * @ClassName: EmailValidateCodeProcessor
 * @Package: com.xinghen.security.core.validate.code.email
 * @Description 邮箱验证码处理器
 * @create 2018-04-18 15:51
 */
@Component
public class EmailValidateCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    @Autowired
    private EmailValidateCodeSender emailValidateCodeSender;

    public EmailValidateCodeProcessor(Map<String, ValidateCodeGenerator> validateCodeGenerators, ValidateCodeRepository validateCodeRepository) {
        super(validateCodeGenerators, validateCodeRepository);
    }

    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        String paramName = SecurityConstants.DEFAULT_PARAMETER_NAME_EMAIL;
        String email = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
        emailValidateCodeSender.send(email, validateCode.getCode());
    }
}
